﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogManager.Application.MultiBlogger.Controls
{
    public partial class Archive : System.Web.UI.UserControl
    {
        private int? SiteId = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["SiteId"]))
                SiteId = Convert.ToInt32(Request.QueryString["SiteId"]);

            if(!IsPostBack)
                loadArchive();
        }

        protected void loadArchive()
        {
            var archiveEntries = (from p in Data.Entities.Blog_Post.All()
                                  where p.SiteId==SiteId
                                  select new
                                             {
                                                 month = p.PublicationDate.Month,
                                                 year = p.PublicationDate.Year
                                             }).Distinct().ToList();
            var group = (from a in archiveEntries
                        group a by new { a.year, a.month }
                            into g
                            select new {g.Key.year, g.Key.month }).OrderBy(x=>x.year).ThenBy(x=>x.month);

            RepeaterForArchiveGroups.DataSource = group;
            RepeaterForArchiveGroups.DataBind();

        }

        protected string monthNumberToName(int monthNumber)
        {
            var monthNames = new List<string>
                                 {
                                     "Empty",
                                     "January",
                                     "Febuary",
                                     "March",
                                     "April",
                                     "May",
                                     "June",
                                     "July",
                                     "August",
                                     "September",
                                     "October",
                                     "November",
                                     "December",
                                 };
            return monthNames[monthNumber];
        }
    }
}